﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using StreetFinderDB;
using StreetFinder;
using System.Text.RegularExpressions;
//using MySql.Data.MySqlClient;
using UnicodeConverter;
//using FastDataAccess.DataAccess;
//using GOL;
using System.Drawing;
using System.Web;
using System.Threading;
using System.Net;

namespace DataAccess
{
    /// <summary>
    /// Summary description for SearchObjectsDB.
    /// </summary>
    /// 
    public class GBDIDInfo
    {
        private int m_nDOLGIS_GRAPHICID;

        public GBDIDInfo()
        {
        }

        public int DOLGIS_GRAPHICID
        {
            get
            {
                return m_nDOLGIS_GRAPHICID;
            }
            set
            {
                m_nDOLGIS_GRAPHICID = value;
            }
        }
    }

    public class GBObjectProvider 
    {
        //Search Service Function
        public string ServiceLink = "http://" + ConfigurationSettings.AppSettings["ServiceHost"] + ":" + ConfigurationSettings.AppSettings["ServiceHost"] + "/Search?";

        const int SRH_NO = 1;		// Bit: 0
        const int SRH_POINT = 256;		// Bit: 8
        const string szTableLocation = "vn_location";

        public GBObjectProvider()
        {
            try
            {
                
                int port = 0;
                if (Global.NumBalanceSearchPort > 1)//-- if have balancing
                {
                    Monitor.Enter(Global.g_objSearchLock);
                    if (Global.PortSearchRange >= Global.NumBalanceSearchPort)
                        Global.PortSearchRange = 0;
                    port = Global.PortSearchRange;
                    ++Global.PortSearchRange;
                    Monitor.Exit(Global.g_objSearchLock);
                }
                port += int.Parse(Global.SearchPort);

                ServiceLink = "http://" + Global.ServiceHost + ":" + port + "/Search?";
            }
            catch
            {
                ServiceLink = "http://" + Global.ServiceHost + ":" + Global.SearchPort + "/Search?";
            }

        }

        
        //Database Function
       /* public void GetLayerType(ref string LayerID, ref string CaptionVN, ref string CaptionEN)
        {
            try
            {
                string sSQL = "Select LayerTypeID,NameVN,NameEN from vn_layer_type order by LayerOrder";
				DataSet ds = ExecuteDataset(sSQL);
                LayerID = CaptionVN = CaptionEN = "['";
				foreach (DataRow row in ds.Tables[0].Rows)
				{
					LayerID += row[0] + "','";
					CaptionVN += row[1] + "','";
					CaptionEN += row[2] + "','";
				}
               

                if (LayerID.Length < 2)
                    return;

                LayerID = LayerID.Substring(0, LayerID.Length - 2) + "]";
                CaptionVN = CaptionVN.Substring(0, CaptionVN.Length - 2) + "]";
                CaptionEN = CaptionEN.Substring(0, CaptionEN.Length - 2) + "]";
            }
            catch { }

        }*/
        /*
        public string GetLayerTypeInHTML(ref string strLayerType)
        {
            try
            {
                string sSQL = "Select LayerTypeID,NameVN,NameEN from vn_layer_type order by LayerOrder";
				DataSet ds = ExecuteDataset(sSQL);
                string sHtml = "<table width=100% height=100%>";
                string row = "";
                strLayerType = "";
				foreach (DataRow dr in ds.Tables[0].Rows)
				{
					row = dr["LayerTypeID"].ToString().Replace(" ", "_");
					strLayerType += row + ",";
					sHtml += "<tr><td>" + "<input type=checkbox id=" + row + " name=" + dr["LayerTypeID"] + " value=" + dr["LayerTypeID"] + " /><span id=G_" + row + " onclick=\"EnableCheck(this)\">" + dr["NameVN"] + "</span></td></tr>";
				}
               
                strLayerType = strLayerType.Substring(0, strLayerType.Length - 1);
                sHtml += "</table>";
                return sHtml;
            }
            catch { return ""; }

        }
         */
        /*
        public List<QueryTuple> GetQueryTuple(string strQuery, int type, int LocAfterComma)
        {
            Converter converter = new Converter();
            Search objSearch = new Search();
            CStreetFinder objStreetFinder = new CStreetFinder();
            strQuery = strQuery.Replace("#", " ");
            strQuery = objSearch.CheckQueryTuple(converter.EncodeUnicode(strQuery), type, LocAfterComma);

            ListQueryTuple lQ = (ListQueryTuple)objStreetFinder.Find(strQuery);
            return lQ.ListOfQueryTuple;
        }
         * /
         /*
        public void GetGeometry(int nId, string strLayer, ref string strCoordinate, ref int nRet)
        {
            string strSQL = "", strCoordinateTemp = "", strLayerGeometry = "";
            int nF = 0, nT = 0, nType = 0;
            Converter converter = new Converter();
            ArrayList list = new ArrayList();
            DataTable tbl;
            DataSet dataset;
            GBDIDInfo info;

            try
            {
                nRet = 1;
                strCoordinate = "";
                strSQL = string.Format("Select Type from {0} where LayerName = '{1}'", szTableLocation, strLayer);
                dataset = ExecuteDataset(strSQL);
                if (dataset != null)
                {
                    tbl = dataset.Tables[0];
                    nType = int.Parse(tbl.Rows[0][0].ToString());
                }
                else
                {
                    strCoordinate = "Error in table 'LOCATION'.";
                    nRet = -1;
                }
            }
            catch
            {
            }

            if (strLayer.IndexOf("_dat") >= 0)
            {
                strLayerGeometry = strLayer.Substring(0, strLayer.IndexOf("_dat"));
                if (strLayerGeometry == "vnisland_province")
                {
                    strSQL = "SELECT DOLGIS_GRAPHICID FROM " + strLayer + " WHERE MAPID = (SELECT MAPID FROM " + strLayer + " WHERE DOLGIS_GRAPHICID = '" + nId.ToString() + "')";
                    try
                    {
                        list = GetAll(strSQL, typeof(GBDIDInfo));
                    }
                    catch
                    {
                        strCoordinate = "Error in table 'PROVINCE'.";
                        nRet = -2;
                    }
                }
                if (strLayerGeometry == "vnisland_district")
                {
                    strSQL = "SELECT DOLGIS_GRAPHICID FROM " + strLayer + " WHERE DISTRICTID = (SELECT DISTRICTID FROM " + strLayer + " WHERE DOLGIS_GRAPHICID = " + nId.ToString() + ")";
                    try
                    {
                        list = GetAll(strSQL, typeof(GBDIDInfo));
                    }
                    catch
                    {
                        strCoordinate = "Error in table 'DISTRICT'.";
                        nRet = -3;
                    }
                }
                if (list.Count > 0)
                {
                    try
                    {
                        for (int nI = 0; nI < list.Count; nI++)
                        {
                            info = (GBDIDInfo)list[nI];
							strSQL = "SELECT " + MultiDBConvert.AsTextFunction("COLLECTION") + " FROM " + strLayerGeometry + " WHERE FID = " + info.DOLGIS_GRAPHICID.ToString();
                            dataset = ExecuteDataset(strSQL);
                            if (dataset != null)
                            {
                                tbl = dataset.Tables[0];
                                strCoordinateTemp = tbl.Rows[0][0].ToString();
                                if (strCoordinate.Length == 0)
                                    strCoordinate = strCoordinateTemp.Substring(0, strCoordinateTemp.Length - 1);
                                else
                                {
                                    nF = strCoordinateTemp.IndexOf("(") + 1;
                                    nT = strCoordinateTemp.Length - nF - 1;
                                    strCoordinate += ("," + strCoordinateTemp.Substring(nF, nT));
                                }
                            }
                        }
                        strCoordinate += ")";
                    }
                    catch
                    {
                        strCoordinate = "Error in table geometry 'PROVINCE & DISTRICT'.";
                        nRet = -4;
                    }
                }
                else if ((nType & SRH_POINT) == 0)
                {
					strSQL = "SELECT " + MultiDBConvert.AsTextFunction("COLLECTION") + " FROM " + strLayerGeometry + " WHERE FID = " + nId.ToString();
                    try
                    {
                        dataset = ExecuteDataset(strSQL);
                        if (dataset != null)
                        {
                            tbl = dataset.Tables[0];
                            strCoordinate = tbl.Rows[0][0].ToString();
                        }
                    }
                    catch
                    {
                        strCoordinate = "Error in table geometry.";
                        nRet = -5;
                    }
                }
            }
        }
         * /
         /*
        public string GetMapCenter(string sDBName)
        {
            string sSQL = "Select CenterX,CenterY,Scale from linkProCodeToDB where DBID like '" + sDBName + "'";
            DataTable tbl = ExecuteDataset(sSQL).Tables[0];
            return tbl.Rows[0][0].ToString() + ":" + tbl.Rows[0][1].ToString() + ":" + tbl.Rows[0][2].ToString();
        }

        public string GetLocationName(long LocationID)
        {
            string sSQL = "Select Namee from LocationInfo where LocationInfoID = " + LocationID;
            return ExecuteScalar(sSQL).ToString().Replace(",", " ").Trim();
        }

        //Interesting Point Access
        public DataSet GetCoordInterestingPoint(string sGuid)
        {
            return new LocationProvider().GetInfo(sGuid);
        }
        */
        //Address Function
        public string IsAddressNumber(string s)
        {
            string strAddNo = "";
            string strAddrNumRegex = "([a-zA-Z|.-]*[0-9]+[\" \"]?(TER|BIS|[a-zA-Z-]*)[\" \"]?(&|/|-|_)?[\" \"]?)*[a-zA-Z|.-]*[0-9]+[\" \"]?(TER|BIS|[a-zA-Z-]*)";
            Regex reg = new Regex(strAddrNumRegex);
            s = s.Trim(new char[] { '-', ' ', ':', '.', '_', ',' });
            Match mc = reg.Match(s);
            if (mc.Success && (mc.Index + mc.Value.Length == s.Length) && mc.Index == 0)// || (mc.Index == 1 && s[0] == '\"')))// do 0 la dau "
            {
                strAddNo = mc.Value;
            }
            return strAddNo;
        }
        /*
        public List<string> IsAddress(ref string strKeyTemp, string strWhereFromTree)
        {
            bool bHaveSign = HaveSign(strKeyTemp);
            Converter converter = new Converter();
            int nIndexOfDollar = strWhereFromTree.IndexOf("$$");
            string[] arrPlacePart;
            string strTreeProv = "", strTreeDist = "", strTreeWard = "";
            if (nIndexOfDollar == 0 && strWhereFromTree != "$$")
            {
                strWhereFromTree = strWhereFromTree.Substring(nIndexOfDollar + 2).Trim(new char[] { ',', ' ' });
                arrPlacePart = strWhereFromTree.Split(',');
                switch (arrPlacePart.Length)
                {
                    case 3:
                        strTreeProv = arrPlacePart[2].Trim();
                        strTreeDist = arrPlacePart[1].Trim();
                        strTreeWard = arrPlacePart[0].Trim();
                        break;
                    case 2:
                        strTreeProv = arrPlacePart[1].Trim();
                        strTreeDist = arrPlacePart[0].Trim();
                        break;
                    case 1:
                        strTreeProv = arrPlacePart[0].Trim();
                        break;
                }
            }
            // Check if it is address

            CStreetFinder objStreetFinder = new CStreetFinder();
            Search objSearch = new Search();
            string strKeyEncoded = converter.EncodeUnicode(strKeyTemp);
            string strQuery = objSearch.CheckAddress(strKeyEncoded);
            AddressEntityResult ae = (AddressEntityResult)objStreetFinder.Find(strQuery);
            string strAddress = "";
            string strAddNo = "";
            List<string> lRes = new List<string>();
            if (ae != null)// && ae.ListOfAddressEntity.Count > 0)
            {
                //TNDang: Latest version
                for (int i = 0; i < ae.ListOfAddressEntity.Count; i++)
                {
                    strAddNo = ae.ListOfAddressEntity[i].AddressNo;
                    strAddress = ae.ListOfAddressEntity[i].OriginalAddress;
                    if (strKeyTemp.IndexOf(strAddNo) == 0 )
                    {
                        strKeyTemp = strAddress;
                        if (ae.ListOfAddressEntity[i].WardName == "" && strTreeWard != "")
                        {
                            ae.ListOfAddressEntity[i].WardName = strTreeWard;
                            strKeyTemp = strKeyTemp + ", " + strTreeWard;
                        }
                        if (ae.ListOfAddressEntity[i].DistrictName == "" && strTreeDist != "")
                        {
                            ae.ListOfAddressEntity[i].DistrictName = strTreeDist;
                            strKeyTemp = strKeyTemp + ", " + strTreeDist;
                        }
                        if (ae.ListOfAddressEntity[i].CityName == "" && strTreeProv != "")
                        {
                            ae.ListOfAddressEntity[i].CityName = strTreeProv;
                            strKeyTemp = strKeyTemp + ", " + strTreeProv;
                        }
                        strAddress = ae.ListOfAddressEntity[i].RegularAddress;
                        if (!bHaveSign)
                        {
                            strAddress = converter.TrimSign(strAddress);
                            lRes.Add(strAddress);
                            return lRes;
                        }
                        else
                            lRes.Add(strAddress);
                    }                    
                }
            }            
            return lRes;
        }
*/
       /*
        public string IsAddress(string strKeyTemp)
        {
            // Check if it is address
            Converter converter = new Converter();
            CStreetFinder objStreetFinder = new CStreetFinder();
            Search objSearch = new Search();
            string strKeyEncoded = converter.EncodeUnicode(strKeyTemp);
            string strQuery = objSearch.CheckAddress(strKeyEncoded);
            AddressEntityResult ae = (AddressEntityResult)objStreetFinder.Find(strQuery);
            string strAddress = "";
            if (ae != null && ae.ListOfAddressEntity.Count > 0)
            {
                strAddress = ae.ListOfAddressEntity[0].OriginalAddress;
                if (strKeyTemp.Length == strAddress.Length)
                {
                    strAddress = ae.ListOfAddressEntity[0].RegularAddress;
                }
                else
                    strAddress = "";
            }
            return strAddress;
        }
        * /
        public AddressEntity AnalizeAddress(string strAddress)
        {
            bool bHaveSign = HaveSign(strAddress);
            Converter converter = new Converter();
            
            string[] arrPlacePart;
            string strTreeProv = "", strTreeDist = "", strTreeWard = "";
            
            CStreetFinder objStreetFinder = new CStreetFinder();
            Search objSearch = new Search();
            string strKeyEncoded = converter.EncodeUnicode(strAddress);
            string strQuery = objSearch.CheckAddress(strKeyEncoded);
            AddressEntityResult ae = (AddressEntityResult)objStreetFinder.Find(strQuery);

            if (ae.ListOfAddressEntity.Count == 0)
                return null;
            else
                return ae.ListOfAddressEntity[0];
        }
        //Support function
        public bool HaveSign(string strInput)
        {
            Converter converter = new Converter();
            string strConverted = converter.TrimSign(strInput);
            return strInput != strConverted;
        }*/
        public static string StandardizeAddressString(string str)
        {
            str = str.Replace(",",", ");
            str = str.Trim(new char[] { ',', ' ', '.' });
            str = Regex.Replace(str, "\\s*/\\s*", "/");
            
            str = Regex.Replace(str, "[^\\w0-9\"/()&.,-]", " ");
            str = Regex.Replace(str, "\\s+", " ");
            return str.ToLower();
        }// Dung cho detect address

        public static string StandardizeString(string str)
        {
            str = str.Replace(",", ", ");
            str = str.Trim(new char[] { ',', ' ', '.' });

            str = Regex.Replace(str, "[^^\\w0-9\"/&.,-°']", " ");
            str = Regex.Replace(str, "\\s+", " ");
            return str;
        }// Dung cho TH binh thuong

      
        public DataSet GetPOIInfo(string Guid)
        {
            string strQuery = ServiceLink + "FindType=GET_POI_INFO";
            strQuery += "&Guid=" + Guid;
            
            return (DataSet)new CStreetFinder().Find(strQuery);
        }
        public DataSet Search(string szKey, string strWhat, string strWhere, int nPage, int nPageSize, double lx, double ly, double rx, double ry, int level, bool bIsLonLat, int type, string field, string source, bool highlight)
        {
            string strQuery = ServiceLink + "FindType=SEARCHING_SEARCH_ALL";
            strQuery += "&Key=" + szKey.Replace("&", "%26");
            strQuery += "&What=" + strWhat.Replace("&", "%26");
            strQuery += "&Where=" + strWhere.Replace("&", "%26");
            strQuery += "&Page=" + nPage;
            strQuery += "&PageSize=" + nPageSize;
            strQuery += "&Lx=" + lx;
            strQuery += "&Ly=" + ly;
            strQuery += "&Rx=" + rx;
            strQuery += "&Ry=" + ry;
            strQuery += "&Level=" + level;
            strQuery += "&IsLonLat=" + bIsLonLat;
            strQuery += "&Type=" + type;
            strQuery += "&Field=" + field;
            strQuery += "&Source=" + source;
            strQuery += "&HightLight=" + highlight.ToString();

            return (DataSet)new CStreetFinder().Find(strQuery);
        }
        public DataSet SearchAll(string szKey, string strWhat, string strWhere, int nPage,  double lx, double ly, double rx, double ry, int level, bool bIsLonLat, int type)
        {
            return SearchAll(szKey, strWhat, strWhere, nPage, 10, lx, ly, rx, ry, level, bIsLonLat, type);
        }
        public DataSet SearchAll(string szKey, string strWhat, string strWhere, int nPage, int nPageSize, double lx, double ly, double rx, double ry, int level, bool bIsLonLat, int type)
        {
            string strQuery = ServiceLink + "FindType=SEARCHING_SEARCH_ALL";
            strQuery += "&Key=" + szKey.Replace("&", "%26");
            strQuery += "&What=" + strWhat.Replace("&", "%26");
            strQuery += "&Where=" + strWhere.Replace("&", "%26");
            strQuery += "&Page=" + nPage;
            strQuery += "&PageSize=" + nPageSize;
            strQuery += "&Lx=" + lx;
            strQuery += "&Ly=" + ly;
            strQuery += "&Rx=" + rx;
            strQuery += "&Ry=" + ry;
            strQuery += "&Level=" + level;
            strQuery += "&IsLonLat=" + bIsLonLat;
            strQuery += "&Type=" + type;

            return (DataSet)FindTimeout(strQuery);
        }

        public object FindTimeout(string strQuery)
        {
            object result = null;

            try
            {
                string strURL = (string)strQuery;
                WebRequest req = WebRequest.Create(strURL);
                req.Timeout = 15000;

                WebResponse resp = req.GetResponse();

                Stream stream = resp.GetResponseStream();

                long len = resp.ContentLength;

                if (len > 0)
                {
                    byte[] data = new byte[len];
                    int nRead = 0;

                    while (nRead < len)
                    {
                        nRead += stream.Read(data, nRead, (int)len - nRead);
                    }

                    result = DataSerializer.DeSerializeBinary(data);
                }

                stream.Close();
                resp.Close();
            }
            catch (Exception ex)
            {
                string strError = ex.Message;
                return null;
            }
            return result;
        }

        public DataSet SearchAddress(string szKey, string szOriKey, int nPage)
        {
            string strQuery = ServiceLink + "FindType=SEARCHING_SEARCH_ADDRESS";
            strQuery += "&Key=" + szKey.Replace("&", "%26");
            strQuery += "&OriKey=" + szOriKey.Replace("&", "%26");
            strQuery += "&Page=" + nPage;

            /* TKV Edit- Anh Nhật sửa lại sau  - 12/9/2011*/
            strQuery += "&nViettelType=0";
            /* End TKV Edit*/

            return (DataSet)new CStreetFinder().Find(strQuery);
        }
        public DataSet SearchPhone(string szKey, int nPage)
        {
            string strQuery = ServiceLink + "FindType=SEARCHING_SEARCH_PHONE";
            strQuery += "&Key=" + szKey.Replace("&", "%26");
            strQuery += "&Page=" + nPage;

            return (DataSet)new CStreetFinder().Find(strQuery);
        }
        
        //SMS Test
        public DataSet SearchSMS(string CateID, string strKey, int nPageSize, string dbLat, string dbLng)
        {
            string strQuery = ServiceLink + "FindType=SEARCHING_SEARCH_SMS";
            strQuery += "&CateID=" + CateID;
            strQuery += "&CateTypeID=2";
            strQuery += "&Key=" + strKey;
            strQuery += "&Page=1";
            strQuery += "&PageSize=" + nPageSize;
            strQuery += "&Places=" + dbLat + "," + dbLng;
            strQuery += "&Radius=5000";

            return (DataSet)new CStreetFinder().Find(strQuery);
        }

        //Category
        public DataSet FindLocationInWardByLatLng(string strAddress, string strCategory, string strCategoryType, int nPage, int nPageSize)
        {
            string strQuery = ServiceLink + "FindType=FIND_LOCATION_IN_WARD_BY_LATLON";

            strQuery += "&Places=" + strAddress;
            strQuery += "&Key=";
            strQuery += "&CateID=" + strCategory;
            strQuery += "&CateTypeID=" + strCategoryType;
            strQuery += "&Page=" + nPage.ToString();
            strQuery += "&PageSize=" + nPageSize.ToString();
            
            return (DataSet)new StreetFinderDB.CStreetFinder().Find(strQuery);
        }

        //Nearby
        public DataSet SearchNearByPoint(string szKey, string strWhat, string szName, string szAddress, string strLng, string strLat, double dbDistance, int nPage)
        {
            string strQuery = ServiceLink + "FindType=SEARCHING_SEARCH_NEARBY_POINT";
            strQuery += "&Key=" + szKey.Replace("&", "%26"); ;
            strQuery += "&What=" + strWhat.Replace("&", "%26"); ;
            strQuery += "&Name=" + szName.Replace("&", "%26"); ;
            strQuery += "&Address=" + szAddress.Replace("&", "%26"); ;
            strQuery += "&Lng=" + strLng;
            strQuery += "&Lat=" + strLat;
            strQuery += "&Distance=" + dbDistance;
            strQuery += "&Page=" + nPage;
            strQuery += "&nViettelType=0" ;

            return (DataSet)new CStreetFinder().Find(strQuery);
        }
        public DataSet SearchNearByPointForBanking(string szKey, string strWhat, string szName, string szAddress, string strLng, string strLat, double dbDistance, int nPage)
        {
            string strQuery = ServiceLink + "FindType=SEARCHING_SEARCH_NEARBY_POINT_FOR_BANKING";
            strQuery += "&Key=" + szKey.Replace("&", "%26");
            strQuery += "&What=" + strWhat.Replace("&", "%26");
            strQuery += "&Name=" + szName.Replace("&", "%26");
            strQuery += "&Address=" + szAddress.Replace("&", "%26");
            strQuery += "&Lng=" + strLng;
            strQuery += "&Lat=" + strLat;
            strQuery += "&Distance=" + dbDistance;
            strQuery += "&Page=" + nPage;

            return (DataSet)new CStreetFinder().Find(strQuery);
        }
       
        /*public DataSet SearchNearbyStreet(double[] dbPoints, string keyword, string strWhat, string strWhere, int nPage, double radius)
        {
            string strQuery = ServiceLink + "FindType=SEARCHING_SEARCH_NEARBY_STREET";
            SearchNearByStreetParam param = new SearchNearByStreetParam();
            param.Points = dbPoints;
            param.Key = keyword;
            param.What = strWhat;
            param.Where = strWhere;
            param.Page = nPage;
            param.PageSize = 10;
            param.Radius = radius;

            return (DataSet)new CStreetFinder().RequestPostXML((object)param, strQuery);
        }
        */
        //Misc
        public DataSet SearchUniversityExam(string strUniversityName, string strAdmissionNo, int nPage)
        {
            string strQuery = ServiceLink + "FindType=SEARCHING_SEARCH_TUYENSINH";
            strQuery += "&UniversityName=" + strUniversityName.Replace("&", "%26"); ;
            strQuery += "&AdmissionNo=" + strAdmissionNo.Replace("&", "%26"); ;
            strQuery += "&Page=" + nPage;
            
            return (DataSet)new CStreetFinder().Find(strQuery);

        }
        public DataSet SearchUniversityExam(string strUniversityName, string strAdmissionNo, int nPage, int nPageSize)
        {
            string strQuery = ServiceLink + "FindType=SEARCHING_SEARCH_TUYENSINH";
            strQuery += "&UniversityName=" + strUniversityName.Replace("&", "%26"); ;
            strQuery += "&AdmissionNo=" + strAdmissionNo.Replace("&", "%26"); ;
            strQuery += "&Page=" + nPage;
            strQuery += "&PageSize=" + nPageSize;

            return (DataSet)new CStreetFinder().Find(strQuery);

        }

        //API
        public PointF SearchAddressForAPI(string szKey)
        {
            string strQuery = ServiceLink + "FindType=SEARCHING_SEARCH_ADDRESS_API";
            strQuery += "&Key=" + szKey.Replace("&", "%26") + "&EndRequest";
            
            return (PointF)new CStreetFinder().Find(strQuery);
        }
        public PointF SearchMapForAPI(string szKey)
        {
            string strQuery = ServiceLink + "FindType=SEARCHING_SEARCH_MAP_API";
            strQuery += "&Key=" + szKey.Replace("&", "%26") + "&EndRequest";

            return (PointF)new CStreetFinder().Find(strQuery);
        }
        
        //Dung cho DS_Store - For Search Address without House number
        public DataSet ListAddressAll(string szKey, string szOriKey, int nPage)
        {
            string strQuery = ServiceLink + "FindType=SEARCHING_LIST_ADDRESS_ALL";
            strQuery += "&Key=" + szKey.Replace("&", "%26"); ;
            strQuery += "&OriKey=" + szOriKey.Replace("&", "%26"); ;
            strQuery += "&Page=" + nPage;

            return (DataSet)new CStreetFinder().Find(strQuery);
        }
        public DataSet ListAddressExceptUserPlace(string szKey, int nPage)
        {
            string strQuery = ServiceLink + "FindType=SEARCHING_LIST_ADDRESS_EXCEPT_USERPLACE";
            strQuery += "&Key=" + szKey.Replace("&", "%26");
            strQuery += "&Page=" + nPage;

            return (DataSet)new CStreetFinder().Find(strQuery);
        }
        /*
		public string GetCoorOfAddressForImportDAB(string strAddress, ref AddressEntity addEntity)
		{
			try
			{
				DataSet ds = null;
				strAddress = StandardizeAddressString(strAddress);
				List<string> lAddress = IsAddressForImportDAB(strAddress, ref addEntity);
				if (lAddress.Count > 0)// It's address
				{
					ds = SearchAddress(lAddress[0], strAddress, 1);
					if (ds == null || ds.Tables[0].Rows.Count <= 0)
					{
						strAddress = strAddress.Substring(strAddress.IndexOf(',') + 1);
						ds = SearchMapsForImportDAB(strAddress);
					}
				}
				else
				{
					List<QueryTuple> listQuery = GetQueryTuple(strAddress, 2, 0);
					if (listQuery != null && listQuery.Count > 0)
						ds = SearchMapsForImportDAB(listQuery[0].strWhere);

				}

				if (ds != null && ds.Tables[0].Rows.Count > 0)
				{
					return "POINT(" + ds.Tables[0].Rows[0]["longitude"].ToString() + " " + ds.Tables[0].Rows[0]["latitude"].ToString() + ")";
				}
				else
					return "POINT(106.700922 10.776572)";
			}
			catch (Exception ex)
			{
				return "";
			}
		}
         
        */
		private DataSet SearchMapsForImportDAB(string szKey)
		{
			CStreetFinder objStreetFinder = new CStreetFinder();
			string strQuery = ServiceLink + "FindType=SEARCHING_SEARCHMAPS_FOR_EDIT_POINT_BANK";
			strQuery += "&strWhere=" + szKey.Replace("&", "%26"); ;
			return (DataSet)objStreetFinder.Find(strQuery);
		}
        /*
		private List<string> IsAddressForImportDAB(string strKey, ref AddressEntity addEntity)
		{
			Converter converter = new Converter();
			CStreetFinder objStreetFinder = new CStreetFinder();
			Search objSearch = new Search();
			bool bHaveSign = HaveSign(strKey);
			string strKeyEncoded = converter.EncodeUnicode(strKey);
			string strQuery = objSearch.CheckAddress(strKeyEncoded);
			AddressEntityResult ae = (AddressEntityResult)objStreetFinder.Find(strQuery);
			string strAddress = "";
			List<string> lRes = new List<string>();
			addEntity = null;
			if (ae != null && ae.ListOfAddressEntity.Count > 0)
			{
				//TNDang: Latest version
				addEntity = ae.ListOfAddressEntity[0];
				for (int i = 0; i < ae.ListOfAddressEntity.Count; i++)
				{
					strAddress = ae.ListOfAddressEntity[i].OriginalAddress;
					if (strKey.Length == strAddress.Length)
					{
						strAddress = ae.ListOfAddressEntity[i].RegularAddress;
						if (!bHaveSign)
						{
							strAddress = converter.TrimSign(strAddress);
							lRes.Add(strAddress);
							return lRes;
						}
						else
							lRes.Add(strAddress);
					}
				}
			}
			return lRes;
		}
          */

        
    }//class

}
